#include <stdio.h>
#include <assert.h>

#include "ctl_srv.h"
#include "broker.h"
#include "common_data.h"

int test_conn(){
  int sock;
  struct sockaddr_in addr;

  addr.sin_family = AF_INET;
  addr.sin_port = htons(CTL_SERVER_PORT);
  //addr.sin_addr.s_addr = htonl(CTL_SERVER_IP);
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  assert((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1);
  assert(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) != -1);

  return sock;
}

int main(){
  
  {
    int sock1 = test_conn();
    
    int val;
    CTL_COMMAND_t cmd;
    flight_task_t tsk = {.hight = 20, .start_time = 0, .hang_time = 10};

    while(1){
      scanf("%d", &val);

      switch(val){
      case 0:
        cmd = CTL_TASK;
        
        int a, b, c;
        //scanf("%d %d %d", &a, &b, &c);
        //tsk.start_time = a * 3600 + b * 60 + c;
        
        assert(send_cmd(sock1, &cmd) != -1);
        assert(send_task(sock1, &tsk) != -1);
        break;
     
      case 1:
        cmd = CTL_INTERRUPT;
     
        assert(send_cmd(sock1, &cmd) != -1);
        break;
      
      case 2:
        cmd = CTL_STOP;
     
        assert(send_cmd(sock1, &cmd) != -1);
        break;

      case 3:
        cmd = CTL_RESUM;
     
        assert(send_cmd(sock1, &cmd) != -1);
        break;

      default:
        break;
      }    
    }

  }
  /*
  sleep(1);
  {
    int sock1 = test_conn();
    CTL_COMMAND_t cmd = CTL_TASK;
    flight_task_t tsk = {.hight = 10, .start_time = 11, .hang_time = 20};

    assert(send_cmd(sock1, &cmd) != -1);
    assert(send_task(sock1, &tsk) != -1);
  }
  sleep(1);
  {
    int sock1 = test_conn();
    CTL_COMMAND_t cmd = CTL_INTERRUPT;

    assert(send_cmd(sock1, &cmd) != -1);
  }
  sleep(1);
  {
    int sock1 = test_conn();
    CTL_COMMAND_t cmd = 12;

    assert(send_cmd(sock1, &cmd) != -1);
  }

  while(1);*/
  
  return 0;
}
